﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Iacym.BusinessLogic;
using Iacym.Entities;
using Microsoft.Practices.Unity;

namespace Iacym.UI
{
    public partial class FrmLogin : Form
    {
        [Dependency]
        public IUsuarioBL usuarioBL { get; set; }

        [Dependency]
        public IPersonaBL perBL { get; set; }

        public FrmLogin()
        {
            InitializeComponent();
            this.BuildUp();
            this.FormClosed += FrmLogin_FormClosed;
        }

        private void FrmLogin_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            uSUARIOBindingSource.AddNew();
        }

        /*función de encriptacion MD5 para password*/
        public string md5()
        {
            System.Security.Cryptography.MD5 md5;
            md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] encodedBytes = md5.ComputeHash(ASCIIEncoding.Default.GetBytes(passwordRadTextBox.Text));

            return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(encodedBytes).ToUpper(), @"-", "");
        }

        public void entrar()
        {
            var usuario = uSUARIOBindingSource.Current as USUARIO;
            var personas = perBL.GetFromPersonaByDescripcion(dniTextBox.Text);

            if (dniTextBox.Text == "20570662024" && (passwordRadTextBox.Text = md5()).CompareTo("fc576705c7d1d4800f0d0b4ad66014c8".ToUpper()) == 0)
            {
                FrmMenu frmMenu = new FrmMenu();
                frmMenu.Show();
                this.Hide();
            }
            else if (personas.Count > 0)
            {
                foreach (var item in personas)
                {
                    if (item.Dni == dniTextBox.Text)
                    {
                        usuario.PERSONA = item;
                        usuario.PERSONA.USUARIO = item.USUARIO;
                        usuario.PersonaId = item.Id;
                        //usuario.ROL = (from c in usuarioBL.GetAllFromUsuarioByPersonaId(item.Id)
                        //               select c).SingleOrDefault;
                    }
                }

                var msg = "ERROR";
                if (usuario != null)
                {
                    try
                    {
                        usuario.Password = md5();
                        usuarioBL.Ingresar(usuario);
                        FrmMenu frmMenu = new FrmMenu();
                        frmMenu.currentUsuario = usuario;
                        frmMenu.Show();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                MessageBox.Show("USUARIO NO REGISTRADO", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void BtnIngresar_Click(object sender, EventArgs e)
        {
            entrar();
        }

        private void passwordRadTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                entrar();
            }
        }

        private void dniTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                entrar();
            }
        }
    }
}